package Assignment08_StringPermutation;

import java.util.ArrayList;
import java.util.List;

public class Assignment08_StringPermutation {
	int alph_in_string;
	int nth_perm;

	ArrayList<String> als = new ArrayList<String>();

	public Assignment08_StringPermutation(int alph_in_string, int nth_perm) {

		this.alph_in_string = alph_in_string;
		this.nth_perm = nth_perm;

	}

	public void getntPermutation() {
		String str = "";

		for (int i = 0; i < alph_in_string; i++) {

			char c = (char) ('a' + i);
			str = str + c;
		}

		generatePerms("", str);
		System.out.println(als);
		System.out.println(als.get(nth_perm - 1));

	}

	private void generatePerms(String incr_str, String decr_sting) {

		if (decr_sting.length() <= 1) {

			als.add(incr_str + decr_sting);
			// System.out.println(incr_str + decr_sting);
		} else {
			for (int i = 0; i < decr_sting.length(); i++) {
				try {

					String newString = decr_sting.substring(0, i)
							+ decr_sting.substring(i + 1);

					generatePerms(incr_str + decr_sting.charAt(i), newString);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

	}

}
